<template>
	<view>
		<u-toast ref="uToast"></u-toast>
		<u--input class="inp" placeholder="请输入userid" border="surround" v-model="value"></u--input>
		<br />
		<u-button @click="login" class="btn" type="primary" text="确定"></u-button>
	</view>
</template>

<script>
	import {
		dingSDK
	} from '@/common/js/dingding.js';
	import {
		mapState,
		mapGetters,
		mapMutations,
		mapActions
	}
	from 'vuex';
	import tabBar from "@/utils/tabBar.js"
	import {
		getUrlParameter
	} from '@/common/js/urils.js';
	import {
		UNITID,
		AR_UNITID,
		CC_UNITID
	} from "@/common/js/warehouse.js"
	import {
		getSysUserByPhones,
		getRelUserByMobile
	} from "@/common/api/ding/di_user.js"
	export default {
		data() {
			return {
				value: '',
				userid: ''
			}
		},
		computed: {
			...mapGetters(['tabBarList', 'APP_USER', 'APP_DEPT', 'APPROVE_USER', 'APPROVE_DEPT', 'JURISDIC_LIST'])
		},
		onLoad() {
			// return
			// const auth_code = this.getAuthCode()
			// console.log("auth_code", auth_code)
			// if (!auth_code) {
			// 	this.$nextTick(() => {
			// 		this.$refs.uToast.show({
			// 			message: '未获取到赣政通授权key',
			// 			type: 'error'
			// 		});
			// 	})
			// 	return
			// }
			// 查询用户信息

		},

		methods: {
			...mapMutations(['user/SET_DING_USER', 'user/SET_APP_DEPT', 'user/SET_APP_USER', 'user/SET_APPROVE_USER',
				'user/SET_APPROVE_DEPT', 'cascade/SET_JURISDIC'
			]),
			...mapActions(['getGztUserInfo', 'getSysUserInfo', 'getJurisdic']),
			// 跳转到行政复议
			goApprove() {
				if (!this.APPROVE_USER.id) {
					this.$refs.uToast.show({
						message: '未获取到审批系统用户信息',
						type: 'error'
					})
					return
				}
				if (!this.APPROVE_DEPT.parentid) {
					this.$refs.uToast.show({
						message: '未获取到审批系统部门信息',
						type: 'error'
					})
					return
				}
				sessionStorage.setItem('tabBarList', JSON.stringify(tabBar['user_approve']))
				uni.navigateTo({
					url: "/pages/approve/home/home"
				})
			},
			login() {
				this.userid = this.value
				this.queryUserInfo()
			},
			// 跳转到社区矫正
			async goCommunity() {
				if (!this.APPROVE_USER.id) {
					this.$refs.uToast.show({
						message: '未获取到审批系统用户信息',
						type: 'error'
					})
					return
				}
				if (!this.APPROVE_DEPT.parentid) {
					this.$refs.uToast.show({
						message: '未获取到审批系统部门信息',
						type: 'error'
					})
					return
				}
				console.log('this.APPROVE_DEPT', this.APPROVE_DEPT)
				const {
					data
				} = await this.getJurisdic(this.APPROVE_DEPT.parentid)
				data && this['cascade/SET_JURISDIC'](data);
				// console.log('JURISDIC_LIST', this.JURISDIC_LIST)
				// 根据身份判断跳转菜单
				if (this.APPROVE_DEPT.id == '3601001004') {
					// 市/区领导
					sessionStorage.setItem('tabBarList', JSON.stringify(tabBar['user_correction_leader']))
					uni.navigateTo({
						url: "/pages/correction/CorrectionHome"
					})
				} else {
					// 区县 / 司法所办事员
					sessionStorage.setItem('tabBarList', JSON.stringify(tabBar['user_correction_clerks']))
					uni.navigateTo({
						url: "/pages/correction/CorrectionHome"
					})
				}
			},
			// 跳转到错误处理页面
			goErrpage() {
				let description = '您所属科室的业务系统待开发'
				uni.navigateTo({
					url: "/pages/errPage?description=" + description
				})
			},
			// 跳转到被申请人答复
			async goRespond() {
				const rel_user = await getRelUserByMobile({
					mobile: this.APP_USER.mobile
				})
				if (rel_user.data) {
					sessionStorage.setItem('tabBarList', JSON.stringify(tabBar['user_correct']))
					uni.navigateTo({
						url: "/pages/respond/pend/pendList"
					})
					return
				}
			},
			// 查询用户信息
			async queryUserInfo(auth_code) {
				let userid = this.userid

				const {
					data
				} = await this.getSysUserInfo([userid, {
					clientId: userid
				}])
				console.log("todo_user", data)
				if (!data) {
					this.$refs.uToast.show({
						message: '未获取到系统用户信息！',
						type: 'error'
					})
					return
				}
				const {
					approveUser,
					approveDepar,
					deptList,
					...sys_user
				} = data
				this['user/SET_APP_DEPT'](deptList);
				this['user/SET_APP_USER'](sys_user);
				if (!(deptList && deptList.length)) {
					this.$refs.uToast.show({
						message: '未获取到用户部门信息！',
						type: 'error'
					})
					return
				}

				this['user/SET_APP_DEPT'](deptList[0]);
				this['user/SET_APP_USER'](sys_user);

				// 审批端人员信息
				approveUser && this['user/SET_APPROVE_USER'](approveUser);
				approveDepar && this['user/SET_APPROVE_DEPT'](approveDepar[0]);
				// console.log('司法钉人员信息', this.APP_USER, this.APP_DEPT)
				// console.log('审批端人员信息', this.APPROVE_USER, this.APPROVE_DEPT)
				// 根据部门信息动态加载菜单
				console.log('动态加载菜单', data.unitId, this.APPROVE_DEPT.sectionid)
				this.goRespond()
				if (data.unitId && data.unitId != UNITID) {
					if (this.APPROVE_DEPT?.sectionid == CC_UNITID) {
						this.goCommunity()
					} else {
						let description = '暂无您需要处理的业务。'
						uni.navigateTo({
							url: "/pages/errPage?description=" + description
						})
					}
					return
				} else {
					console.log('审批端人员信息', this.APPROVE_USER, this.APPROVE_DEPT)
					if (this.APPROVE_DEPT?.sectionid == AR_UNITID) {
						this.goApprove()
					} else if (this.APPROVE_DEPT?.sectionid == CC_UNITID) {
						this.goCommunity()
					} else {
						this.goErrpage()
					}
				}
			}
		}
	}
</script>

<style>
</style>